package com.ltx.mcpserver.service;

import com.ltx.mcpserver.model.ArticleRequestDTO;
import com.ltx.mcpserver.model.ArticleResponseDTO;
import com.ltx.mcpserver.tools.MarkdownConverter;
import jakarta.annotation.Resource;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Service;
import retrofit2.Response;

import java.io.IOException;
import java.util.Collections;

/**
 * @author 罗添煦
 * @description Csdn自动发帖服务
 * @create 2025-04-22 下午1:42
 */
@Service
public class CsdnService {

    @Resource
    private ICSDNService icsdnService;

    @Tool(description = "帮我发送一篇博客到csdn上")
    public String getCsdnPost(@ToolParam(description = "文章标题") String title,
                              @ToolParam(description = "文章内容") String markdowncontent,
                              @ToolParam(description = "文章标签") String tags,
                              @ToolParam(description = "文章简述") String description) throws IOException {
        // 1. 构建请求对象
        ArticleRequestDTO request = new ArticleRequestDTO();
        request.setTitle(title);
        request.setMarkdowncontent(markdowncontent);
        request.setContent(MarkdownConverter.convertToHtml(markdowncontent));
        request.setReadType("public");
        request.setLevel("0");
        request.setTags(tags);
        request.setStatus(0);
        request.setCategories("后端");
        request.setType("original");
        request.setOriginal_link("");
        request.setAuthorized_status(true);
        request.setDescription(description);
        request.setResource_url("");
        request.setNot_auto_saved("0");
        request.setSource("pc_mdeditor");
        request.setCover_images(Collections.emptyList());
        request.setCover_type(0);
        request.setIs_new(1);
        request.setVote_id(0);
        request.setResource_id("");
        request.setPubStatus("draft");
        request.setSync_git_code(0);

        String cookie = "p_uid=U010000; Hm_up_6bcd52f51e9b3dce32bec4a3997715ac=%7B%22islogin%22%3A%7B%22value%22%3A%221%22%2C%22scope%22%3A1%7D%2C%22isonline%22%3A%7B%22value%22%3A%221%22%2C%22scope%22%3A1%7D%2C%22isvip%22%3A%7B%22value%22%3A%220%22%2C%22scope%22%3A1%7D%2C%22uid_%22%3A%7B%22value%22%3A%22qq_62325622%22%2C%22scope%22%3A1%7D%7D; Hm_lvt_6bcd52f51e9b3dce32bec4a3997715ac=1714928935,1714983060; _clck=ndwe7u%7C2%7Cflj%7C0%7C1586; cf_clearance=i9hsVClbctmM02U8C8xVDZ2wOZApdU_cT9V3fIhZZhk-1715167297-1.0.1.1-rCJMioSw0h4OajLhZXNP9tHu1PHLxeEMQYn2PV6858d1rvZlty2G4zVo1JlIXAxtQvjwfYaxBQcrePy70Ukl7w; chat-version=2.1.1; cf_clearance=ujODD56LAdRZa8RBWaVBZ.RzLgR8KjGUWZNH66L0spk-1716647877-1.0.1.1-cfR9vJip6LqP9h5.pLrdZ8gYRrZxgka55KWEij0noJA6maymA4JD2Bd3LLNc_k4.bs48R3gmvyMX9RDefdZvWg; c_ins_fpage=/index.html; c_ins_um=-; ins_first_time=1720287142905; fid=20_69962459855-1723127564530-385451; c_ins_fref=-; UN=qq_62325622; ssxmod_itna=YqRx2D0DBi0=uixl4iwr88GOiAxydkDqqGNqG8mDnqD=GFDK40oEH3PeExNnpIb/nGEx4GoD7urmeYW+GpbXKY1ePoDU4i8DCTuAoD48KGwD0eG+DD4DWDmeHDnxAQDjxGPc0EHv=DEDYPZDit6D7UVeeDjIT=RKeG0DDUFn4G2Qj=eDDXtSGG0AGDxxGY6KCtdwTqb=0DcD0tqxBdWIPxcmmFkrYNbOcaDdhQDzw7DtLUKhubDCE6Zjq01AAmxUbvYmDpTn0DKADx=iSfxi8xmn7qz9h5P/G+Xng+gpDxxD; c_ins_prid=1728632755089_195462; c_ins_rid=1731562538330_925714; uuid_tt_dd=10_36624159550-1734507808579-479266; UserName=qq_62325622; UserInfo=01821ae901f04fe183ccb859a482876b; UserToken=01821ae901f04fe183ccb859a482876b; UserNick=%E7%94%A8%E8%8D%89%E4%B9%A6%E8%B0%B1%E5%86%99%E5%85%B0%E4%BA%AD%E5%BA%8F; AU=6A0; BT=1737529000352; csdn_newcert_qq_62325622=1; c_adb=1; historyList-new=%5B%5D; c_dl_prid=1736731087092_756522; c_dl_rid=1744361125355_889799; c_dl_fref=https://so.csdn.net/so/search; c_dl_fpage=/download/weixin_38751014/14039188; c_dl_um=distribute.pc_search_result.none-task-blog-2%7Eall%7Efirst_rank_ecpm_v1%7Erank_v31_ecpm-3-138740459-null-null.142%5Ev102%5Epc_search_result_base3; c_ab_test=1; c_segment=2; dc_sid=33b37a2a52c43d392dc00f0fa216de77; dc_session_id=10_1745248805388.389654; c_first_ref=default; c_first_page=https%3A//www.csdn.net/; c_dsid=11_1745248805833.239800; c-sidebar-collapse=0; c_utm_term=lombok@data%E6%B3%A8%E8%A7%A3%E4%B8%8D%E8%B5%B7%E4%BD%9C%E7%94%A8; fe_request_id=1745249137249_2069_2292331; creativeSetApiNew=%7B%22toolbarImg%22%3A%22https%3A//img-home.csdnimg.cn/images/20230921102607.png%22%2C%22publishSuccessImg%22%3A%22https%3A//img-home.csdnimg.cn/images/20240229024608.png%22%2C%22articleNum%22%3A50%2C%22type%22%3A2%2C%22oldUser%22%3Atrue%2C%22useSeven%22%3Afalse%2C%22oldFullVersion%22%3Atrue%2C%22userName%22%3A%22qq_62325622%22%7D; c_pref=https%3A//www.csdn.net/; c_ref=https%3A//so.csdn.net/so/search%3Fspm%3D1000.2115.3001.7498%26q%3Dlombok%2540data%25E6%25B3%25A8%25E8%25A7%25A3%25E4%25B8%258D%25E8%25B5%25B7%25E4%25BD%259C%25E7%2594%25A8%26t%3D%26u%3D%26utm_term%3Dlombok%2540data%25E6%25B3%25A8%25E8%25A7%25A3%25E4%25B8%258D%25E8%25B5%25B7%25E4%25BD%259C%25E7%2594%25A8%26utm_medium%3Ddistribute.pc_toolbar_associateword.none-task-associate_word-opensearch_query-4-lombok%2540data%25E6%25B3%25A8%25E8%25A7%25A3%25E4%25B8%258D%25E8%25B5%25B7%25E4%25BD%259C%25E7%2594%25A8-null-null.179%255Ev5%255Epv%26depth_1-utm_source%3Ddistribute.pc_toolbar_associateword.none-task-associate_word-opensearch_query-4-lombok%2540data%25E6%25B3%25A8%25E8%25A7%25A3%25E4%25B8%258D%25E8%25B5%25B7%25E4%25BD%259C%25E7%2594%25A8-null-null.179%255Ev5%255Epv%26request_id%3D246b137b557f5608cc83d8b8a80a21d9%26opensearch_request_id%3D246b137b557f5608cc83d8b8a80a21d9; c_utm_source=cknow_so_nontop_query; utm_source=cknow_so_nontop_query; log_Id_click=6; c_utm_relevant_index=1; relevant_index=1; c_utm_medium=distribute.pc_search_result.none-task-blog-2%7Eall%7Etop_positive%7Edefault-1-146037083-null-null.142%5Ev102%5Epc_search_result_base3; creative_btn_mp=3; referrer_search=1745249143597; c_page_id=default; log_Id_pv=5; dc_tos=sv2qy2; log_Id_view=239";

        retrofit2.Call<ArticleResponseDTO> call = icsdnService.saveArticle(request, cookie);
        Response<ArticleResponseDTO> response = call.execute();

        // 3. 验证结果
        if (response.isSuccessful()) {
            ArticleResponseDTO articleResponseDTO = response.body();
            return articleResponseDTO.getData().toString();
        } else if (response.code() == 400) {
            return "请检查cookie是否正确";
        } else {
            return "未知错误";
        }
    }

}
